home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UCRASM25.ARJ / STRCMP.ASM < prev    next >
Assembly Source File  |  1991-10-12  |  2KB  |  102 lines

  1. StdGrp        group    stdlib,stddata
  2. stddata        segment    para public 'sldata'
  3. stddata        ends
  4. ;
  5. stdlib        segment    para public 'slcode'
  6.         assume    cs:stdgrp
  7. ;
  8. ;
  9. ; strcmp- Compares two strings.
  10. ;
  11. ; inputs:
  12. ;
  13. ;    es:di-    First string (The string to compare)
  14. ;    dx:si-    Second string (The string to compare against)
  15. ;
  16. ;    e.g.,
  17. ;        "if (es:di < dx:si) then ..."
  18. ;
  19. ; returns: 
  20. ;
  21. ;    cx- index into strings where they differ (points at the zero byte
  22. ;        if the two strings are equal).
  23. ;    Condition codes set according to the string comparison.  You should
  24. ;    use the unsigned branches (ja, jb, je, etc.) after calling this
  25. ;    routine.
  26. ;
  27.         public    sl_strcmp
  28. ;
  29. sl_strcmp    proc    far
  30.         push    es
  31.         push    ds
  32.         push    bx
  33.         push    ax
  34.         push    si
  35.         push    di
  36. ;
  37. ; Swap pointers so they're more convenient for the LODSB/SCASB instrs.
  38. ;
  39.         xchg    si, di
  40.         mov    ax, es
  41.         mov    ds, ax
  42.         mov    es, dx
  43. ;
  44.         xor    bx, bx        ;Set initial index to zero.
  45. ;
  46. ; In order to preserve the direction flag across this call, we have to
  47. ; test whether or not it is set here and execute two completely separate
  48. ; pieces of code (so we know which state to exit in.  Unfortunately, we
  49. ; cannot use pushf to preserve this flag since we need to return status
  50. ; info in the other flags.
  51. ;
  52.         pushf
  53.         pop    ax
  54.         test    ah, 4        ;Test direction bit.
  55.         jnz    DirIsSet
  56. sclp:        lodsb
  57.         scasb
  58.         jne    scNE        ;If strings are <>, quit.
  59.         inc    bx            ;Increment index into strs.
  60.         cmp    al, 0        ;Check for end of strings.
  61.         jne    sclp
  62.         pushf
  63.         dec    bx
  64.         popf
  65. ;
  66. scNE:        pop    di
  67.         pop    si
  68.         mov    cx, bx
  69.         pop    ax
  70.         pop    bx
  71.         pop    ds
  72.         pop    es
  73.         ret            ;Return with direction flag clear.
  74. ;
  75. ;
  76. DirIsSet:    lodsb
  77.         scasb
  78.         jne    scNE2         ;If strings are <>, quit.
  79.         inc    bx
  80.         cmp    al, 0         ;Check for end of strings.
  81.         jne    DirIsSet
  82.         pushf
  83.         dec    bx
  84.         popf
  85. ;
  86. scNE2:        pop    di
  87.         pop    si
  88.         mov    cx, bx
  89.         pop    ax
  90.         pop    bx
  91.         pop    ds
  92.         pop    es
  93.         std            ;Return with direction flag set.
  94.                 ret
  95.  
  96. sl_strcmp    endp
  97. ;
  98. ;
  99. stdlib        ends
  100.         end
  101.